package com.zc.controller;

import cn.hutool.crypto.digest.MD5;
import com.github.pagehelper.PageInfo;
import com.zc.annotation.SystemLogAnnotation;
import com.zc.pojo.SysAccount;
import com.zc.pojo.SysRole;
import com.zc.service.SysAccountService;
import com.zc.service.SysRoleService;
import com.zc.utils.JSONResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Controller
@RequestMapping("/sysAccount")
public class SysAccountController {

    @Autowired
    private SysAccountService sysAccountService;

    @Autowired
    private SysRoleService sysRoleService;

    @SystemLogAnnotation("账号信息跳转列表页面")
    @GetMapping("toList")
    public String toList() {
        return "sysAccount/sysAccountList";
    }

    @SystemLogAnnotation("账号信息列表查询")
    @GetMapping("list")
    @ResponseBody
    public JSONResult list(String realName, String email, String createTimeRange, Integer page, Integer limit) {
        Map<String, Object> data = new HashMap<>();
        String startDate = null;
        String endDate = null;
        if (StringUtils.isNotEmpty(createTimeRange)) {
            String[] date = createTimeRange.split(" - ");
            startDate = date[0];
            endDate = date[0];
        }
        PageInfo<SysAccount> pageInfo = sysAccountService.queryByPage(realName, email, startDate, endDate, page, limit);

        List<SysAccount> sysAccountList = pageInfo.getList();

        data.put("sysAccountSize", pageInfo.getTotal());
        data.put("sysAccounts", sysAccountList);
        return JSONResult.ok(data);
    }

    @SystemLogAnnotation("账号信息跳转新增页面")
    @GetMapping("toAdd")
    public String toAdd(Model model) {

        List<SysRole> sysRoleList = sysRoleService.queryAll();
        model.addAttribute("sysRoleList", sysRoleList);

        return "sysAccount/sysAccountAdd";
    }

    @SystemLogAnnotation("账号信息新增")
    @PostMapping("add")
    @ResponseBody
    public JSONResult Add(@RequestBody SysAccount sysAccount) {

        String password = sysAccount.getPassword();
        String salt = UUID.randomUUID().toString().replace("-", "");
        MD5 md5 = new MD5(salt.getBytes());
        String digestHex = md5.digestHex(password);
        sysAccount.setPassword(digestHex);
        sysAccount.setSalt(salt);
        sysAccountService.save(sysAccount);

        return JSONResult.ok();
    }

    @SystemLogAnnotation("账号信息跳转修改页面")
    @GetMapping("toUpDate/{id}")
    public String toUpData(@PathVariable Long id, Model model) {
        SysAccount sysAccount = sysAccountService.getById(id);
        List<SysRole> sysRoleList = sysRoleService.queryAll();
        model.addAttribute("sysRoleList", sysRoleList);
        model.addAttribute("sysAccount", sysAccount);
        return "sysAccount/sysAccountUpDate";
    }

    @SystemLogAnnotation("账号信息修改")
    @PutMapping("update")
    @ResponseBody
    public JSONResult upDate(@RequestBody SysAccount sysAccount) {

        sysAccountService.update(sysAccount);

        return JSONResult.ok();
    }

    @SystemLogAnnotation("账号信息删除")
    @DeleteMapping("delete/{id}")
    @ResponseBody
    public JSONResult delete(@PathVariable Long id) {

        sysAccountService.logicalDelete(id);

        return JSONResult.ok();
    }

    @SystemLogAnnotation("账号信息跳转详情页面")
    @GetMapping("toDetail/{id}")
    public String toDetail(@PathVariable Long id, Model model) {
        SysAccount sysAccount = sysAccountService.getById(id);
        model.addAttribute("sysAccount", sysAccount);
        return "sysAccount/sysAccountDetail";
    }
}